/* Initially taken from http://www.java2s.com/Code/Java/Swing-JFC/Panelwithbackgroundimage.htm */

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

class ImagePanel extends JPanel
{
    private Image img;

    // returns the dimensions of the image
    private Dimension get_dimensions (Image img)
    {
        return new Dimension (img.getWidth(null), img.getHeight(null));
    }

    // sets the current size and prevents resizing
    private void set_sizes (Dimension size)
    {
        setPreferredSize (size);
        setMinimumSize (size);
        setMaximumSize (size);
        setSize (size);
        setLayout (null);
    }

    // ctors
    public ImagePanel (String img)
    {
        this (new ImageIcon(img).getImage());
    }

    public ImagePanel (Image img)
    {
        setLayout (null);
        set_image (img);
    }

    // force repaint
    public void refresh ()
    {
        this.validate ();
        this.repaint ();
    }

    // updates the image
    public void set_image (Image img)
    {
        this.img = img;
        set_sizes (get_dimensions (img));

        refresh();
    }
	
	// retrieves the image
    public Image get_image ()
    {
        return img;
    }

    // paint method
    public void paintComponent(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(img, 0, 0, null);
    }
}
